<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>语音识别测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .test-container {
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 20px;
            margin: 20px 0;
        }
        button {
            padding: 10px 20px;
            margin: 5px;
            background: #1890ff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background: #40a9ff;
        }
        button:disabled {
            background: #d9d9d9;
            cursor: not-allowed;
        }
        .status {
            margin: 10px 0;
            padding: 10px;
            border-radius: 4px;
        }
        .success {
            background: #f6ffed;
            border: 1px solid #b7eb8f;
            color: #52c41a;
        }
        .error {
            background: #fff2f0;
            border: 1px solid #ffccc7;
            color: #ff4d4f;
        }
        .info {
            background: #e6f7ff;
            border: 1px solid #91d5ff;
            color: #1890ff;
        }
    </style>
</head>
<body>
    <h1>语音识别测试页面</h1>
    <p>这个页面用于测试语音识别功能是否正常工作。</p>
    
    <div class="test-container">
        <h3>语音识别测试</h3>
        <button id="startBtn" onclick="startRecognition()">开始语音识别</button>
        <button id="stopBtn" onclick="stopRecognition()" disabled>停止识别</button>
        <button onclick="testWebSpeech()">测试Web Speech API</button>
        
        <div id="status" class="status info" style="display: none;">
            状态信息将显示在这里
        </div>
        
        <div id="result" style="margin-top: 20px;">
            <h4>识别结果：</h4>
            <div id="resultText" style="padding: 10px; background: #f5f5f5; border-radius: 4px; min-height: 50px;">
                识别结果将显示在这里
            </div>
        </div>
    </div>

    <script>
        let recognition = null;
        let isRecognizing = false;

        // 检查浏览器支持
        if ('webkitSpeechRecognition' in window || 'SpeechRecognition' in window) {
            const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
            recognition = new SpeechRecognition();
            
            recognition.continuous = false;
            recognition.interimResults = false;
            recognition.lang = 'zh-CN';
            
            recognition.onstart = () => {
                showStatus('语音识别已开始，请说话...', 'info');
                isRecognizing = true;
                document.getElementById('startBtn').disabled = true;
                document.getElementById('stopBtn').disabled = false;
            };
            
            recognition.onresult = (event) => {
                const transcript = event.results[0][0].transcript;
                showStatus('识别成功！', 'success');
                document.getElementById('resultText').textContent = transcript;
            };
            
            recognition.onend = () => {
                showStatus('语音识别结束', 'info');
                isRecognizing = false;
                document.getElementById('startBtn').disabled = false;
                document.getElementById('stopBtn').disabled = true;
            };
            
            recognition.onerror = (event) => {
                let errorMsg = '语音识别错误: ' + event.error;
                if (event.error === 'no-speech') {
                    errorMsg = '未检测到语音输入，请确保麦克风正常工作并说话';
                }
                showStatus(errorMsg, 'error');
                isRecognizing = false;
                document.getElementById('startBtn').disabled = false;
                document.getElementById('stopBtn').disabled = true;
            };
        } else {
            showStatus('您的浏览器不支持语音识别功能', 'error');
        }

        function startRecognition() {
            if (!recognition) {
                showStatus('语音识别未初始化', 'error');
                return;
            }
            
            if (isRecognizing) {
                showStatus('语音识别正在进行中，请勿重复启动', 'error');
                return;
            }
            
            try {
                recognition.start();
            } catch (error) {
                showStatus('启动语音识别失败: ' + error.message, 'error');
            }
        }

        function stopRecognition() {
            if (recognition && isRecognizing) {
                recognition.stop();
            }
        }

        function testWebSpeech() {
            if ('speechSynthesis' in window) {
                const utterance = new SpeechSynthesisUtterance('语音合成测试，如果您能听到这段话，说明语音功能正常工作。');
                utterance.lang = 'zh-CN';
                speechSynthesis.speak(utterance);
                showStatus('语音合成测试已开始', 'info');
            } else {
                showStatus('您的浏览器不支持语音合成功能', 'error');
            }
        }

        function showStatus(message, type) {
            const statusDiv = document.getElementById('status');
            statusDiv.textContent = message;
            statusDiv.className = 'status ' + type;
            statusDiv.style.display = 'block';
        }
    </script>
</body>
</html>

